import {StringUtils} from "@/business/utils/string.utils";
import type {GraphService} from "@/business/interface/graph.interface";
import {ProxyGraphServiceList} from "@/business/service/graph.service";
import type {Graph} from "@/business/entity/graph/graph.entity";
import {DrawAnnotation} from "@/business/annontation/annotation.aop";

/**
 *  绘图代理类
 * @author JiangXincan
 * @date 2023/01/28 18:23
 */
export class ProxyGraphService<T extends GraphService> {

    private readonly _ctx: CanvasRenderingContext2D;

    private _graphService: T;

    /**
     * 代理类初始化时，通过panelID 选择对应的绘图实现类，并创建该类对象
     * @param ctx 绘图上下文
     * @param className 动态类名称
     * @param option 动态类创建参数
     */
    constructor(ctx: CanvasRenderingContext2D, className: string, option?:any) {
        this._ctx = ctx;
        //判断动态类的Key，也就是panelId是否存在
        if (StringUtils.empty(className)) {
            throw new Error(`未找到 className：${className} 对应实现`);
        }
        // 根据panelId下标，判断对应的具体代理类是否存在
        // @ts-ignore
        if (ProxyGraphServiceList[className] === undefined || ProxyGraphServiceList[className] === null) {
            throw new Error(`未找到 className：${className} 对应实现`);
        }
        //找到后，对其实例化，并限定该类父级类型
        // @ts-ignore
        this._graphService = new ProxyGraphServiceList[className]!<GraphService>(option!);
        // this._graphService =  ProxyGraphServiceList[className];
    }

    /**
     * 通过代理绘制图形
     * @param graph 绘制图形实体类
     */
    @DrawAnnotation.DrawGraph()
    public graphDraw(graph: Graph) {
        console.log(`绘图【绘制阶段】：进行绘制${graph.id}图形`);
        this._graphService.create(this._ctx, graph)
    }

    get ctx() {
        return this._ctx;
    }

}